#!/bin/sh
#
# 简单写一下脚本的功能。
#
#
#

if [ $# -ne 0 ]; then
	echo "Usage: this is a empty shell."
	exit
fi

echo "today: " `date -I`

cd /Users/hbaolong/workshop/gitee/sixgod

rm -f 1-6.md 1-3.md 4-6.md zengsong.md weidian.csv yd.csv

mv /Users/hbaolong/Downloads/se1*.csv weidian.csv 2>/dev/null

mv /Users/hbaolong/Downloads/Goods*.csv yd.csv 2>/dev/null

rm -f /Users/hbaolong/Downloads/1-6.csv

rm -f /Users/hbaolong/Downloads/1-3.csv

rm -f /Users/hbaolong/Downloads/4-6.csv

if [ -e weidian.csv ]; then 

	cat weidian.csv |awk -F, '{print $1, " " $17 " " $18 " " $27 " " $77}' >`date -I`.tmp.md

	#awk '{print $3}' `date -I`.tmp.md | sort -g |uniq -c |awk '{print $NF}'

	grep "124539303196" `date -I`.tmp.md|awk -F: '{print $NF ",长期有效"}' >1-6.md

	grep "124606862351" `date -I`.tmp.md|awk -F: '{print $NF ",长期有效"}' >>1-6.md

	grep "124539137195" `date -I`.tmp.md|awk -F: '{print $NF ",长期有效"}' >1-3.md

	grep "124607026350" `date -I`.tmp.md|awk -F: '{print $NF ",长期有效"}' >>1-3.md

	grep "124539137197" `date -I`.tmp.md|awk -F: '{print $NF ",长期有效"}' >4-6.md

	grep "124607026352" `date -I`.tmp.md|awk -F: '{print $NF ",长期有效"}' >>4-6.md

	cat module.md 1-6.md >tmp.md ;mv tmp.md 1-6.csv
	cat module.md 1-3.md >tmp.md ;mv tmp.md 1-3.csv
	cat module.md 4-6.md >tmp.md ;mv tmp.md 4-6.csv

	mv 1-6.csv 1-3.csv 4-6.csv /Users/hbaolong/Downloads

fi

if [ -e yd.csv ]; then
	cat yd.csv | awk -F, '{print $1, " " $8 " " $23 }'|grep ^E >yd.tmp.md

	grep "1-6" yd.tmp.md |awk '{print $NF ",长期有效"}' >>1-6.md
	grep "1-6" yd.tmp.md |awk '{print $NF ",长期有效"}' >>zengsong.md
	grep "1-3" yd.tmp.md |awk '{print $NF ",长期有效"}' >>1-3.md
	grep "4-6" yd.tmp.md |awk '{print $NF ",长期有效"}' >>4-6.md

	cat module.md 1-6.md >tmp.md ;cp tmp.md zengsong.csv ;mv tmp.md 1-6.csv
	cat module.md 1-3.md >tmp.md ;mv tmp.md 1-3.csv
	cat module.md 4-6.md >tmp.md ;mv tmp.md 4-6.csv

	mv 1-6.csv 1-3.csv 4-6.csv zengsong.csv /Users/hbaolong/Downloads

fi


echo "请在小鹅通导入下列文件： 1-6.csv  1-3.csv  4-6.csv"

if [ -s 1-6.md ]; then 

	echored "有全集需要开通！"
	for files in `cat 1-6.md `
	do
		grep $files 已经开通成功.md >/dev/null
		if [ $? -ne 0 ];then 
			echo $files >> 已经开通成功.md
		fi
	done
else
	echo "无全集需要开通！"
fi 

if [ -s 1-3.md ]; then 
	echored "有初阶需要开通！"
	for files in `cat 1-3.md `
	do
		grep $files 已经开通成功.md
		if [ $? -ne 0 ];then 
			echo $files >> 已经开通成功.md
		fi
	done
else
	echo "无初阶需要开通！"
fi

if [ -s 4-6.md ]; then 
	echored "有高阶需要开通！"
	for files in `cat 4-6.md `
	do
		grep $files 已经开通成功.md
		if [ $? -ne 0 ];then 
			echo $files >> 已经开通成功.md
		fi
	done
else
	echo "无高阶需要开通！"
fi

rm -f ok.md

touch ok.md

if [ -e weidian.csv ]; then

	echo "请导入下列已经发货订单(微店)" >>ok.md

	awk '{print $1 "\t" $2 "\t" $3 }' `date -I`.tmp.md >>ok.md
else 
	echo "没有微店订单！"
fi

if [ -e yd.csv ]; then 

	echo "请导入下列已经发货订单(有赞)" >>ok.md
	
	awk '{print $1 "\t其他\t" $NF }' yd.tmp.md >>ok.md

else 
	echo "没有有赞订单！"
fi

if [ -s ok.md ]; then
	subl ok.md
fi
